package com.dabai.iot.core.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dabai.common.core.domain.R;
import com.dabai.common.mybatis.core.page.PageQuery;
import com.dabai.common.mybatis.core.page.TableDataInfo;
import com.dabai.common.web.core.BaseController;
import com.dabai.iot.core.dto.DevicePropertyQuery;
import com.dabai.iot.core.model.DeviceProperty;
import com.dabai.iot.core.service.IDevicePropertyService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 设备属性记录 控制器
 *
 * @author kev1n
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/device/property")
public class DevicePropertyController extends BaseController {

    private final IDevicePropertyService devicePropertyService;

    /**
     * 最新的属性数据
     */
    @GetMapping("/latest")
    @Operation(summary = "最新的属性数据", description = "传入对应查询参数")
    public R<List<DeviceProperty>> lastProperties(@RequestParam Long blockId, @RequestParam Long deviceId) {
        return R.ok(devicePropertyService.getLastProperties(blockId, deviceId));
    }

    /**
     * 属性历史数据分页
     */
    @GetMapping("/history")
    @Operation(summary = "属性历史数据分页", description = "传入对应查询参数")
    public TableDataInfo<Map<String, Object>> page(PageQuery pageQuery, @Validated DevicePropertyQuery propertyQuery) {
        IPage<Map<String, Object>> pages = devicePropertyService.getPropertyLogPageFromTD(pageQuery.build(), propertyQuery);
        return TableDataInfo.build(pages);
    }

}
